#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "../include/meta.h"
#include "../include/vfs_errorcodes.h"

void changep(node*);

char* mvdir(char src[50],char dest[50])
{	
	 
	node *srcnode= (node *) malloc(sizeof(node));
	node *destnode = (node *) malloc(sizeof(node));
	node *rootnode = (node *) malloc(sizeof(node));
	node *prev= (node *) malloc(sizeof(node));
	node *temp,*folder;
	rootnode = root;
	char result[50];
	
	char *token;
	token=strtok(src,"/");
	
	if(strcmp(root->data,token)!=0)
		{
		
		return ERR_VFS_MOVEDIR_01;
		}
	
	while(token!=NULL)
	{
		if(rootnode==NULL)
		{
			
		return ERR_VFS_MOVEDIR_03;
		}

		else if(strcmp(rootnode->data,token)==0) // match
		{
		 prev = rootnode;
		 rootnode=rootnode->lchild;	 
		}
		else if(strcmp(rootnode->data,token)!=0) // no match
		{

		while((rootnode->rchild!=NULL)&&(strcmp(rootnode->data,token)!=0))
			rootnode=rootnode->rchild;
		 if(strcmp(rootnode->data,token)==0)
			{
			prev = rootnode;
			rootnode=rootnode->lchild;
			}
		}
	token=strtok(NULL,"/");
	}

	srcnode = prev;
	
	rootnode = root;
	token=strtok(dest,"/");
	if(strcmp(root->data,token)!=0)
		{
		
		 return ERR_VFS_MOVEDIR_02;
		}
	
	while(token!=NULL)
	{
	if(rootnode==NULL)
		{
			
			return ERR_VFS_MOVEDIR_04;
		}
			
	else if(strcmp(rootnode->data,token)==0) // match
		{
		 prev = rootnode;
		 rootnode=rootnode->lchild;	 
		}
	else if(strcmp(rootnode->data,token)!=0) // no match
		{
		while((rootnode->rchild!=NULL)&&(strcmp(rootnode->data,token)!=0))
			rootnode=rootnode->rchild;
		 if(strcmp(rootnode->data,token)==0)
			{
			prev = rootnode;
			rootnode=rootnode->lchild;
			}
		}
	token=strtok(NULL,"/");
	}

	destnode = prev;

	// moving a folder into itself
	folder=destnode;
	temp=folder->parent;
	while(folder!=NULL && temp!=srcnode && temp!= NULL)
	{	folder=folder->parent;
		temp=folder->parent;
	}
	if(temp!=NULL && temp==srcnode)
	{	//strcpy(result,"Cannot move a folder into itself");
		return ERR_VFS_MOVEDIR_06;
	}

	// src is lchild
	temp=srcnode->parent;
	if(srcnode= temp->lchild)
		temp->lchild= srcnode->rchild;
	
	// src is rchild
	else{ 
		prev=temp->lchild;
		while(prev->rchild!=srcnode)
		{ prev=prev->rchild;}
		prev->rchild=srcnode->rchild;
	}
	srcnode->rchild = NULL;

	//actual moving code
	if(destnode->lchild==NULL)
		{destnode->lchild=srcnode;
		 srcnode->parent=destnode;
		}

	 else
		{
		
		 destnode=destnode->lchild;
		 while(destnode->rchild!=NULL && destnode->data!=srcnode->data)
		{
			destnode=destnode->rchild;
		}
		if(destnode->data==srcnode->data)
		{
		return ERR_VFS_MOVEDIR_05;
		}
		 destnode->rchild=srcnode;
		 srcnode->parent=destnode->parent;
		}

	displayTree(root);
	changep(srcnode);
	
	return "ok";

}

void changep(node* srcnode)
{
	if(srcnode!=NULL)
	{
	node* temp=(node*) malloc(sizeof(node));
	char str1[50],path[50],new_path[50];
	
	str1[0]='/';
	str1[1] = '\0';
	char filename[20];
	
	int i;int j=0;int k=0;int m=0;
	int result1,hashvalue;

	strcpy(filename,fd[srcnode->index].file_name);
	for (i=1;fd[srcnode->index].file_name[j]!='\0';i++)
	{
		str1[i] = fd[srcnode->index].file_name[j];
		j++;
	}
	str1[i]='\0';
	
	path[0] = '\0';
	for (i=0;fd[srcnode->index].location_path[k]!='\0';i++)
	{
		path[i] = fd[srcnode->index].location_path[k];
		k++;
	}
	path[i]='\0';
	
	//delete hash
	
		hashvalue=hash(filename);
		result1=deletion(hashvalue,filename,path);
	
		temp=srcnode->parent;
		strcat(fd[temp->index].location_path,str1);

	//deletion of bst node
	strcpy(fd[srcnode->index].location_path,fd[temp->index].location_path);
	
	new_path[0] = '\0';
	for (i=0;fd[srcnode->index].location_path[m]!='\0';i++)
	{
		new_path[i] = fd[srcnode->index].location_path[m];
		m++;
	}
	new_path[i]='\0';
	printf("path name3:  %s\n",new_path);

	//use insert hash
	insert_into_list(hashvalue,filename,new_path);
	printf("hash insert over \n");
	
	//recursive call to change sub folders
	changep(srcnode->lchild);
	changep(srcnode->rchild);
	
	}
}





